/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package judf.chart;

import java.awt.Font;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;

/**
 *
 * @author Frank Gabriel Rodríguez Lee
 */
public class FabricaChart {
    //La lista de datos a enviar debe tener el formato de Datos(valor, clase, tipo)
    public static JFreeChart createLineChart( List datos, String titulo, String subtitulo, String xname, String yname, String fuente ) {
        CategoryDataset dataset = ChartDataSet.getCategoryDataSet( datos );
        JFreeChart chart = ChartFactory.createLineChart(
            titulo, // chart title
            xname, // domain axis label
            yname, // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            false, // include legend
            true, // tooltips
            false // urls
        );
        chart.addSubtitle(new TextTitle(subtitulo));
        TextTitle source = new TextTitle(fuente);
        source.setFont(new Font("SansSerif", Font.PLAIN, 10));        
        source.setPosition(RectangleEdge.BOTTOM);
        source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
        chart.addSubtitle(source);
        chart.setBackgroundPaint(Color.white);
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.white);
        // customise the range axis...
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        // customise the renderer...
        LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
        renderer.setShapesVisible(true);
        renderer.setDrawOutlines(true);
        renderer.setUseFillPaint(true);
        renderer.setFillPaint(Color.white);
        return chart;
    }

    public static JPanel createLineChartPanel( List datos, String titulo, String subtitulo, String xname, String yname, String fuente ) {
        JFreeChart chart = createLineChart(datos, titulo, subtitulo, xname, yname, fuente);
        ChartPanel cp = new ChartPanel( chart );
        return cp;
    }


    public static void main( String[] args ){
        List dat = new ArrayList(1);
        List temp1 = new ArrayList(1);
        List temp2 = new ArrayList(1);

        temp1.add(20);
        temp1.add("Clase");
        temp1.add("Periodo 1");

        temp2.add(30);
        temp2.add("Clase");
        temp2.add("Periodo 2");

        dat.add(temp1);
        dat.add(temp2);

        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.add(FabricaChart.createLineChartPanel( dat, "Historial de Consumos", "", "Periodos", "Volumen Consumido(m3)", "Tocfac" ));
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }   
}
